from django.shortcuts import redirect, render, HttpResponse
from rbac import models
from rbac.form.menu import MenuForm, tmenu_func_form


def menu_list(request):
    '''
    菜单展示
    :param request:
    :return:
    '''
    if request.method == 'GET':
        # 二级菜单的menu_id
        mid = request.GET.get('mid')
        menu_tow_dict = {}
        if not mid:
            # 没有点击一级菜单，默认展示所有信息
            permission_queryset = models.Permission.objects.all().values('id', 'parent_id', 'name', 'title', 'url',
                                                                         'menu_id')
            for item in permission_queryset:
                parent_id = item.get('parent_id')
                # 如果没有parent_id时，直接进行构造
                if not parent_id:
                    menu_tow_dict[item['id']] = {
                        'title': item['title'],
                        'id': item['id'],
                        'url': item['url'],
                        'name': item['name'],
                        'menu_id': item['menu_id'],
                        'children': []
                    }
            # print(menu_tow_dict)
            for item in permission_queryset:
                parent_id = item.get('parent_id')
                # print(parent_id)
                if parent_id:
                    menu_tow_dict[parent_id]['children'].extend([
                        {
                            'title': item['title'],
                            'id': item['id'],
                            'url': item['url'],
                            'name': item['name'],
                            'menu_id': item['menu_id'],
                        }
                    ])
            # print(menu_tow_dict)
        else:
            # 有mid，渲染成自己的二级菜单与其所属的功能模块
            # 现在二级菜单拿到了,
            permission_queryset = models.Permission.objects.filter(menu_id=mid).values('id', 'name',
                                                                                       'title', 'url',
                                                                                       'menu_id')

            for item in permission_queryset:
                # 进行二级菜单作为父亲，数据构造
                menu_tow_dict[item['id']] = {
                    'title': item['title'],
                    'id': item['id'],
                    'url': item['url'],
                    'name': item['name'],
                    'menu_id': item['menu_id'],
                    'children': []
                }
            # print(menu_tow_dict)
            for item in permission_queryset:
                # 二级构造完成之后，进行功能模块数据构造
                parent_id = item.get('id')
                # 功能菜单数据
                children = models.Permission.objects.filter(parent_id=parent_id).values('title', 'id', 'url', 'name',
                                                                                        'menu_id')
                for child in children:
                    # 以parent_id为key找到其父级。进行数据添加
                    menu_tow_dict[parent_id]['children'].extend([
                        {
                            'title': child['title'],
                            'id': child['id'],
                            'url': child['url'],
                            'name': child['name'],
                            'menu_id': child['menu_id'],
                        }
                    ])
            print(menu_tow_dict)

        menu_queryset = models.Menu.objects.all()
        return render(request, 'rbac/menu_list.html', locals())


def menu_edit(request, menu_id):
    '''
    菜单编辑
    :param request:
    :return:
    '''
    menu_obj = models.Menu.objects.filter(pk=menu_id).first()
    if request.method == 'GET':
        form = MenuForm(instance=menu_obj)
        return render(request, 'rbac/menu_edit.html', locals())
    form = MenuForm(data=request.POST, instance=menu_obj)
    if form.is_valid():
        form.save()
        return redirect('menu_list')
    return render(request, 'rbac/menu_edit.html', locals())


def menu_add(request):
    '''
    菜单删除
    :param request:
    :return:
    '''
    if request.method == 'GET':
        form = MenuForm()
        return render(request, 'rbac/menu_add.html', locals())
    form = MenuForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect('menu_list')
    return render(request, 'rbac/menu_add.html', locals())


def menu_del(request, menu_id):
    menu_obj = models.Menu.objects.filter(pk=menu_id).first().delete()
    return redirect('menu_list')


def tmenu_func_add(request):
    '''
    二级菜单添加&功能模块添加
    :param request:
    :return:
    '''
    if request.method == 'GET':
        form = tmenu_func_form()
        return render(request, 'rbac/tmenu_func_add.html', locals())
    form = tmenu_func_form(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect('menu_list')
    return render(request, 'rbac/tmenu_func_add.html', locals())


def tmenu_func_edit(request, pk):
    '''
    二级菜单添加&功能模块编辑
    :param request:
    :return:
    '''
    menu_obj = models.Permission.objects.filter(pk=pk).first()
    if request.method == 'GET':
        # print(request.path_info)
        form = tmenu_func_form(instance=menu_obj)
        return render(request, 'rbac/tmenu_func_edit.html', locals())
    form = tmenu_func_form(data=request.POST, instance=menu_obj)
    if form.is_valid():
        form.save()
        # print(request.path_info)
        return redirect('menu_list')
    return render(request, 'rbac/tmenu_func_edit.html', locals())


def tmenu_func_del(request, pk):
    menu_obj = models.Permission.objects.filter(pk=pk).first().delete()
    return redirect('menu_list')
